package com.freesun.shop.listener;

import com.freesun.shop.model.WechatMessage;
import com.freesun.shop.service.WechatPushService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

@Component
public class ValidationCodeListener implements MessageListener{
    private static Logger logger = LoggerFactory.getLogger(ValidationCodeListener.class);
    /**
     * destination  监听的队列
     * concurrency  启动几个线程来监听
     * @param message
     */
    @Autowired
    private WechatPushService wechatPushService;
    @Override
    @JmsListener(destination = "validation.code.queue",concurrency = "1")
    public void onMessage(Message message) {
        logger.info("收到消息了:{}", message);
        try {
            ObjectMessage objectMessage= (ObjectMessage) message;
           WechatMessage msg= (WechatMessage) objectMessage.getObject();
            wechatPushService.pushMsg(msg);
            message.acknowledge();
            logger.info("消息消费成功！");
        } catch (JMSException e) {
            logger.info("消息消费失败！");
            e.printStackTrace();
        }

    }
}
